package qr.core;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.strategy.TreeStrategy;
import org.simpleframework.xml.strategy.Type;
import org.simpleframework.xml.stream.NodeMap;

@Root
public class Leaderboard {
	
	private long id; 
	
	@ElementList
	private List<Player> players;
	
	public Leaderboard() {
		this.players = new ArrayList<Player>();
	}
	
	/** 
	 * Method to get leaderboard 
	 * @return - list of players
	 */
	public List<Player> getPlayers() { 
		return players; 
	}
	
	public void setPlayers(List<Player> players) {
		this.players = players;
	}
	
	public void addPlayer(Player player) { 
		
		players.add(player);
	}
	
	public long getId() {
		return this.id;
	}
	
	public String toXML(){
		players = new ArrayList<Player>( players );
		Serializer serializer = new Persister( new TreeStrategy(){
			
            public boolean write(Type field, Object value, NodeMap node, Map map) {
                if( field.getType().getName().contains("objectdb") ){
            		node.put("class", field.getType().getName().replace("objectdb.", ""));
                }
                return false;
            } 
			
		});
		
		StringWriter writer = new StringWriter();
		try {
			serializer.write(this, writer);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return writer.getBuffer().toString();
	}
}
